package com.sx.mvvmlibs.event

/**
 * @Author: YY
 * @Date: 2022/1/8
 * @Describe: 通用的 UI 事件
 */
@Suppress("UNCHECKED_CAST")
class UIChangeLiveData {

    // 刷新
    var refreshEvent: SingleLiveEvent<Boolean>? = null
    // 加载更多
    var loadEvent: SingleLiveEvent<Boolean>? = null

    // 显示Dialog
    var showLoadingDialogEvent: SingleLiveEvent<String?>? = null
    // 隐藏Dialog
    var dismissLoadingDialogEvent: SingleLiveEvent<Any?>? = null

    // 启动Activity
    var startActivityEvent: SingleLiveEvent<Map<String, Any>>? = null
    // 启动并结束Activity
    var startActivityFinishEvent: SingleLiveEvent<Map<String, Any>>? = null
    // 启动盛装Activity
    var startContainerFragmentEvent: SingleLiveEvent<Map<String, Any>>? = null
    // 结束Activity
    var finishEvent: SingleLiveEvent<Void>? = null

    fun initRefreshEvent(): SingleLiveEvent<Boolean?> {
        refreshEvent = createLiveData(refreshEvent)
        return refreshEvent as SingleLiveEvent<Boolean?>
    }

    fun initLoadEvent(): SingleLiveEvent<Boolean?> {
        loadEvent = createLiveData(loadEvent)
        return loadEvent as SingleLiveEvent<Boolean?>
    }

    fun getLoadingDialogEvent(): SingleLiveEvent<String?> {
        showLoadingDialogEvent = createLiveData(showLoadingDialogEvent)
        return showLoadingDialogEvent as SingleLiveEvent<String?>
    }

    fun getDismissDialogEvent() : SingleLiveEvent<Any?> {
        dismissLoadingDialogEvent = createLiveData(dismissLoadingDialogEvent)
        return dismissLoadingDialogEvent as SingleLiveEvent<Any?>
    }

    fun initStartActivityEvent(): SingleLiveEvent<Map<String, Any>> {
        startActivityEvent = createLiveData(startActivityEvent)
        return startActivityEvent as SingleLiveEvent<Map<String, Any>>
    }

    fun initStartActivityFinishEvent(): SingleLiveEvent<Map<String, Any>> {
        startActivityFinishEvent = createLiveData(startActivityFinishEvent)
        return startActivityFinishEvent as SingleLiveEvent<Map<String, Any>>
    }

    fun initStartContainerFragmentEvent(): SingleLiveEvent<Map<String, Any>> {
        startContainerFragmentEvent = createLiveData(startContainerFragmentEvent)
        return startContainerFragmentEvent as SingleLiveEvent<Map<String, Any>>
    }

    fun initFinishEvent() : SingleLiveEvent<Void> {
        finishEvent = createLiveData(finishEvent)
        return finishEvent as SingleLiveEvent<Void>
    }

    @Suppress("NAME_SHADOWING")
    private fun <T> createLiveData(liveData: SingleLiveEvent<T>?): SingleLiveEvent<T> {
        var liveData: SingleLiveEvent<T>? = liveData
        if (liveData == null) {
            liveData = SingleLiveEvent()
        }
        return liveData
    }
}